﻿using System.ComponentModel;
using System.Threading;
using JY.Wpf.Core.Tools;

namespace JY.Wpf.Core.ViewModelBases
{
    /// <summary>
    /// 子窗体ViewModelBase
    /// </summary>
    public abstract class ChildWindowViewModelBase : ViewModelBase
    {
        #region # 变量
        #endregion

        #region # 构造器
        /// <summary>
        /// 构造器
        /// </summary>
        protected ChildWindowViewModelBase()
        {
            this.Width = 1000;
        }
        #endregion

        #region # 属性
        #region 是否打开——bool IsOpen
        private bool _isOpen;
        /// <summary>
        /// 是否打开
        /// </summary>
        public bool IsOpen
        {
            get { return this._isOpen; }
            set
            {
                this._isOpen = value;
                if (!this._isOpen)
                {
                    this.Deactivate();
                }
                base.NotifyOfPropertyChange();
            }
        }
        #endregion

        #region 宽度——double Width
        private double _width;
        /// <summary>
        /// 宽度
        /// </summary>
        public double Width
        {
            get { return this._width; }
            set { this._width = value; base.NotifyOfPropertyChange(); }
        }

        #endregion

        #region 高度——double Height
        private double _height;
        /// <summary>
        /// 高度
        /// </summary>
        public double Height
        {
            get { return this._height; }
            set { this._height = value; base.NotifyOfPropertyChange(); }
        }
        #endregion
        #endregion

        #region # 方法 
        #region # 显示窗体——void Show()
        /// <summary>
        /// 显示窗体
        /// </summary>
        public virtual void Show()
        {
            this.IsOpen = true;
            this.OnActivateAsync(CancellationToken.None);
        }
        #endregion

        #region # 关闭窗体中——void ChildWindowClosing(CancelEventArgs args)
        /// <summary>
        /// 关闭窗体中
        /// </summary>
        /// <param name="args">取消事件参数</param>
        public virtual void ChildWindowClosing(CancelEventArgs args)
        {

        }
        #endregion

        #region # 关闭窗体——void Close()
        /// <summary>
        /// 关闭窗体
        /// </summary>
        public virtual void Close()
        {
            this.IsOpen = false;
            this.TryCloseAsync(true);
        }
        #endregion

        #region # 页面失活——void Deactivate()
        /// <summary>
        /// 页面失活
        /// </summary>
        public override void Deactivate()
        {
            this.EventAggregator.Unsubscribe(this);
            this.TryCloseAsync(true);
            base.Deactivate();
        }
        #endregion
        #endregion
    }
}
